# ------------------------------------------------------------------
# Metview macro with a user interface to make the addition of
# News examples easier.
#
# It assumes that there exist a Notes icon and an examples folder
# for the news item.
#
# Execute the macro, edit the item's name, notes icon and
# example folder, click apply and the news item should now
# appear in the list of available articles.
#
# Tests can be performed by setting the environment variable
# METVIEW_NEWS to another folder, restarting Metview and working
# within that folder.
#
# Iain Russell
# 27th July 2004
# ------------------------------------------------------------------



# -------------------------------------
# Part 1 - retrieve the user's choices
# -------------------------------------


# define the user interface components

a = any (
			name	: "News Item Name",
			default	: "New News Item"
		)

b = any (
			name	: "Notes Icon",
			default	: "articles/New News Item"
		)

c = any (
			name	: "Examples Folder",
			default	: "examples/New News Item"
		)


# retrieve the input values

input = dialog([a, b, c])

if input <> nil then
	Item = 
	(
		strName     : input["News Item Name"],
		strNotes    : ParseFilePath (input["Notes Icon"]),
		strExamples : ParseFilePath (input["Examples Folder"])
	)
else
	fail("macro failed to get input elements")
end if

print (Item.strName, "   ", Item.strNotes, "   ", Item.strExamples)



# -------------------------------------
# Part 2 - check that everything exists
# -------------------------------------

if (not (exist (Item.strNotes))) then
    fail ("Could not find notes file "     & Item.strNotes)
end if

if (not (exist (Item.strExamples))) then
    fail ("Could not find example folder " & Item.strExamples)
end if



# --------------------------------------
# Part 3 - ensure everything is readable
# --------------------------------------

shell ("chmod a+rx " & Item.strNotes)
shell ("chmod a+rx " & GetDotFile (Item.strNotes))

shell ("chmod -R a+rx " & Item.strExamples)
shell ("chmod -R a+rx " & GetDotFile (Item.strExamples))


# --------------------------------
# Part 4 - create the ARTICLE file
# --------------------------------

fArticle = file (Item.strName)
write (fArticle, "ARTICLE,", newline)
write (fArticle, "    NOTE       = '", Item.strNotes,    "',", newline)
write (fArticle, "    EXAMPLES   = '", Item.strExamples, "'",  newline)
write (fArticle, newline)
fArticle = 0


fDotArticle = file (GetDotFile (Item.strName))
write (fDotArticle, "NOTE,", newline)
write (fDotArticle, "    ICON_NAME  = ", Item.strName, ",", newline)
write (fDotArticle, "    ICON_CLASS = ARTICLE,",            newline)
write (fDotArticle, "    RENAMABLE  = TRUE,",               newline)
write (fDotArticle, "    X          = 36,",                 newline)
write (fDotArticle, "    Y          = 10",                  newline)
write (fDotArticle, newline)
fDotArticle = 0


# -------------------
# Auxillary functions
# -------------------


# ----------------------------------------------------------------------------
# Function      : ParseFilePath
#
# Author (date) : Iain Russell (27/07/2004)
#
# Description   : The problem is this: if the user enters a path name with
#                 directory separators, the user interface code interprets
#                 it as a list, not a string. This function turns it back
#                 into a string.
#
# ----------------------------------------------------------------------------

function ParseFilePath (in_path)

    # if it's already a string, then just return it unaltered

    if (type(in_path) = "string") then
        return in_path
    
    # otherwise, if it's a list, then combine elements with dir separator
        
    else if (type(in_path) = "list") then
    
        strPath = in_path [1]


        for i = 2 to count(in_path) do
	        strPath = strPath & '/' & in_path [i]	        
        end for
        
        return strPath
        
    
    # number? then convert to a string (not working, but unlikely to happen

    else if (type(in_path) = "number") then
        return "" & in_path
    
    
    # something else? then we are not prepared for it...
    
    else
        fail ("Input path must be a string")
    end if


end ParseFilePath




# ----------------------------------------------------------------------------
# Function      : GetDotFile
#
# Author (date) : Iain Russell (27/07/2004)
#
# Description   : Returns the name of the dot file associated with the given
#                 file. Example "my_file" returns ".my_file".
#                 "a/b/c/file" returns "a/b/c/.file".
#
# ----------------------------------------------------------------------------

function GetDotFile (strOriginalPath)

    # convert the path into a list of components

    lDirs = parse (strOriginalPath, "/")
    

    # start with the filename, 'dotted'

    strPath = "." & lDirs [count (lDirs)]


    # now prepend the dirs to the filename

    for i = count (lDirs) - 1 to 1 by -1 do
	    strPath = lDirs [i] & '/' & strPath
    end for


    return strPath

end GetDotFile




